<template>
  <div class="register-container">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" label-position="left">

      <div class="title-container">
        <h3 class="title">后台管理系统</h3>
      </div>

      <!-- 用户名 -->
      <el-form-item prop="username" class="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="registerForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
        />
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password" class="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registerForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!-- 确认密码 -->
      <el-form-item prop="confirmPwd" class="confirmPwd">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="confirmPwd"
          v-model="registerForm.confirmPwd"
          :type="confirmPwdType"
          placeholder="Confirm Password"
          name="confirmPwd"
          tabindex="2"
        />
        <span class="show-pwd" @click="showConfirmPwd">
          <svg-icon :icon-class="confirmPwdType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!-- 邮箱 -->
      <el-form-item prop="email" class="email">
        <span class="svg-container">
          <svg-icon icon-class="email" />
        </span>
        <el-input
          v-model="registerForm.email"
          placeholder="Email"
          tabindex="2"
        />
      </el-form-item>
      <!-- 验证码 -->
      <el-form-item prop="code" class="code">
        <div>
          <span class="svg-container">
            <svg-icon icon-class="code" />
          </span>
          <el-input
            v-model="registerForm.code"
            placeholder="Code"
            tabindex="2"
            @keyup.enter.native="handleRegister"
          />
        </div>
        <el-button type="primary" @click="sendRegisterCode">发送验证码</el-button>
      </el-form-item>

      

      <div id="forget-password">
        <span @click="toLogin">登录</span>
      </div>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">注册</el-button>

      <!-- <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div> -->

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import user from '@/api/user'

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码应大于6个字符'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {},
      registerRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      confirmPwdType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 隐藏与显示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 隐藏与显示确认密码
    showConfirmPwd() {
      if (this.confirmPwdType === 'password') {
        this.confirmPwdType = ''
      } else {
        this.confirmPwdType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 获取验证码
    sendRegisterCode() {
      console.log("------------", this.registerForm.email)
      user.sendRegisterCode(this.registerForm.email).then(res => {
        if (res.code == 200) {
          this.$message.success("获取验证码成功，请前往邮箱查看")
        }
      })
    },
    // 注册
    handleRegister() {
      // 表单验证
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          user.register(this.registerForm).then(res => {
            console.log(res)
            if (res.code == 200) {
              this.$message.success("注册成功，为您跳转到登录界面")
              this.toLogin()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 前往登录界面
    toLogin() {
      this.$router.push({ path: '/login' })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.register-container {
  // 验证码样式
  .code {
    height: 50px;
    .el-form-item__content {
      display: flex;
      div {
        flex: 1;
      }
      img {
        height: 50px;
      }
    }
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>


<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.register-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  // 设置背景图
  background: url(../../assets/bg.jpg) no-repeat center;
  background-size: cover;
  // 设置登录表单垂直水平居中
  display: flex;
  align-items: center;
  justify-content: center;

  .register-form {
    width: 700px;
    position: relative;
    min-width: 520px;
    max-width: 100%;
    padding: 45px 75px 15px;
    overflow: hidden;
    border-radius: 15px;
    overflow: hidden;
    z-index: 100;

    // 忘记密码
    #forget-password {
      float: right;
      color: rgba(255, 255, 255, 0.5);
      margin: 15px 5px 15px 0;
      cursor: pointer;
    }
    #forget-password span:hover {
      color: #8679f6;
    }

    // 登录按钮
    .el-button {
      background: linear-gradient(90deg, #32CCFF 0%, #00E138 50%,#00E138 100%);
      border: none;
      opacity: .9;
      height: 50px;
      border-radius: 5px;
    }
  }

  // 登录框毛玻璃效果
  .register-form::before {
    content: "";
    width: calc(100% + 20px);
    height: calc(100% + 20px);
    position: absolute;
    top: -10px;
    left: -10px;
    overflow: hidden;
    background: inherit;
    box-shadow: inset 0 0 0 200px rgba(90, 90, 90, 0.5);
    filter: blur(10px);
    border: 1px solid white;
    z-index: -1;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  // 图标样式
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>